package com.hera.base.web.vaadin.mantenimiento;

import static com.hera.base.util.InputData.getStringData;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.Constantes;
import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.base.web.IConfirmDialog;
import com.hera.base.web.MantenimientoMaestro;
import com.hera.process.config.domain.Datasource;
import com.hera.process.config.rest.delegate.ConfigDelegate;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class DatasourceUI extends BaseUI implements MantenimientoMaestro,
		ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private HorizontalLayout hlyBotonera;
	@AutoGenerated
	private Button btnTestConnection;
	@AutoGenerated
	private Button btnEliminar;
	@AutoGenerated
	private Button btnGuardar;
	@AutoGenerated
	private TextField txtSchema;
	@AutoGenerated
	private TextField txtJndi;
	@AutoGenerated
	private Label lbltitulo;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfMaestro;
	/**
	 * 
	 */
	private static final long serialVersionUID = 190072366270912343L;
	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	public DatasourceUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

	}

	private static final Logger log = Logger.getLogger(DatasourceUI.class);

	@Autowired
	ConfigDelegate configDelegate;
	boolean esNuevo;
	Datasource datasource;

	@Override
	public void buttonClick(ClickEvent event) {
		removeStyleNotValidMaestroControllers();
		if (event.getButton().equals(btnEliminar)) {
			eliminarMaestro();
		} else if (event.getButton().equals(btnGuardar)) {
			guardarMaestro();
		}

	}

	@Override
	public void cargarFormulario() {

		txtJndi.setNullRepresentation("");
		txtSchema.setNullRepresentation("");
		txtJndi.setMaxLength(50);
		txtSchema.setMaxLength(30);

		log.info("INCIO +++++++++");
		esNuevo = true;
		datasource = null;

		btnEliminar.setVisible(false);
		btnEliminar.addClickListener(this);
		
		btnTestConnection.setVisible(false);
		btnTestConnection.addClickListener(this);

		btnGuardar.setVisible(false);
		btnGuardar.addClickListener(this);
		
		tblfMaestro.setTitulo(" Lista Data Source ");
		tblfMaestro.setHeight("200px");

		tblfMaestro.addColumn("jndiName", "JNDI", 250, true, String.class);
		tblfMaestro.addColumn("schema", "ESQUEMA", 150, true, String.class);

		tblfMaestro.construir();
		tblfMaestro.addTextChangeListener(new TextChangeListener() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);

			}
		});

		tblfMaestro.addValueChangeListener(new ValueChangeListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				removeStyleNotValidMaestroControllers();
				datasource = (Datasource) tblfMaestro.getSelectedItem(event);
				esNuevo = (datasource == null);
				tblfMaestro.limpiarFiltro();
				if (esNuevo) {
					limpiarMaestro();
					btnEliminar.setVisible(false);
					btnTestConnection.setVisible(false);
				} else {
					btnEliminar.setVisible(true);
					btnTestConnection.setVisible(true);
					
					txtJndi.setValue(datasource.getJndiName());
					txtSchema.setValue(datasource.getSchema());
				}
				log.info("Fin Cargar Formulario DATASOURCE ");

			}
		});

	}

	public void removeStyleNotValidMaestroControllers(){
		txtJndi.removeStyleName(Constantes.VALIDACION_CAMPOS.CAMPO_REQUERIDO_VACIO);
		txtSchema.removeStyleName(Constantes.VALIDACION_CAMPOS.CAMPO_REQUERIDO_VACIO);
	}
	
	@Override
	public void buscarMaestro() {
		buscarMaestro(null);
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio Buscar DataSource");
		FiltroDTO filtroDTO = tblfMaestro.getFiltroDTO(event);
		List<Datasource> lstDataSource = configDelegate.findAllDatasource(filtroDTO);
		tblfMaestro.setLstData(lstDataSource);
		limpiarMaestro();
		log.info("fin FindDatasource");
	}

	@Override
	public void limpiarMaestro() {
		log.info("inicio limpiarMaestro");
		btnGuardar.setVisible(true);		
		txtJndi.setValue("");
		txtSchema.setValue("");
		log.info("fin limpiarMaestro");
	}

	@Override
	public void eliminarMaestro() {
		log.info("inicio eliminarMaestro");
		mostrarConfirmacion(new IConfirmDialog() {			
			@Override
			public void cancelar(Button event, Window window) {
				window.close();
			}			
			@Override
			public void aceptar(Button event, Window window) {
				window.close();
				configDelegate.deleteDatasource(datasource);
				buscarMaestro();
				limpiarMaestro();
			}
		});		
		log.info("fin eliminarMaestro");
	}

	@Override
	public void guardarMaestro() {
		log.info("inicio guardarMaestro");
		if (esNuevo) {
			datasource = new Datasource();
			obtenerDatosFormulario(datasource);
			configDelegate.createDatasource(datasource);
		} else {
			obtenerDatosFormulario(datasource);
			configDelegate.updateDatasource(datasource);
		}
		limpiarMaestro();
		buscarMaestro();
		log.info("fin guardarMaestro");
	}

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();

	}

	private void obtenerDatosFormulario(Datasource datasource) {
		datasource.setJndiName(getStringData(txtJndi, "Nombre JNDI"));
		datasource.setSchema(getStringData(txtSchema, "Esquema"));
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("-1px");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("-1px");
		setHeight("-1px");
		
		// verticalLayout_1
		verticalLayout_1 = buildVerticalLayout_1();
		mainLayout.addComponent(verticalLayout_1);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_1() {
		// common part: create layout
		verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("-1px");
		verticalLayout_1.setHeight("-1px");
		verticalLayout_1.setMargin(false);
		verticalLayout_1.setSpacing(true);
		
		// lytMaestro
		lytMaestro = buildLytMaestro();
		verticalLayout_1.addComponent(lytMaestro);
		
		return verticalLayout_1;
	}

	@AutoGenerated
	private HorizontalLayout buildLytMaestro() {
		// common part: create layout
		lytMaestro = new HorizontalLayout();
		lytMaestro.setImmediate(false);
		lytMaestro.setWidth("-1px");
		lytMaestro.setHeight("-1px");
		lytMaestro.setMargin(false);
		lytMaestro.setSpacing(true);
		
		// lytTablaMaestra
		lytTablaMaestra = buildLytTablaMaestra();
		lytMaestro.addComponent(lytTablaMaestra);
		
		// pnlFormulario
		pnlFormulario = buildPnlFormulario();
		lytMaestro.addComponent(pnlFormulario);
		
		return lytMaestro;
	}

	@AutoGenerated
	private HorizontalLayout buildLytTablaMaestra() {
		// common part: create layout
		lytTablaMaestra = new HorizontalLayout();
		lytTablaMaestra.setImmediate(false);
		lytTablaMaestra.setWidth("470px");
		lytTablaMaestra.setHeight("-1px");
		lytTablaMaestra.setMargin(false);
		
		// tblfMaestro
		tblfMaestro = new TableCustom();
		tblfMaestro.setImmediate(false);
		tblfMaestro.setWidth("-1px");
		tblfMaestro.setHeight("-1px");
		lytTablaMaestra.addComponent(tblfMaestro);
		
		return lytTablaMaestra;
	}

	@AutoGenerated
	private VerticalLayout buildPnlFormulario() {
		// common part: create layout
		pnlFormulario = new VerticalLayout();
		pnlFormulario.setImmediate(false);
		pnlFormulario.setWidth("-1px");
		pnlFormulario.setHeight("-1px");
		pnlFormulario.setMargin(false);
		pnlFormulario.setSpacing(true);
		
		// lbltitulo
		lbltitulo = new Label();
		lbltitulo.setStyleName("titulo");
		lbltitulo.setImmediate(false);
		lbltitulo.setWidth("-1px");
		lbltitulo.setHeight("-1px");
		lbltitulo.setValue("Data Source");
		pnlFormulario.addComponent(lbltitulo);
		pnlFormulario.setComponentAlignment(lbltitulo, new Alignment(20));
		
		// txtJndi
		txtJndi = new TextField();
		txtJndi.setStyleName("fieldRequired");
		txtJndi.setImmediate(false);
		txtJndi.setCaption("Nombre JNDI: *");
		txtJndi.setWidth("200px");
		txtJndi.setHeight("-1px");
		txtJndi.setInputPrompt("Nombre JNDI");
		pnlFormulario.addComponent(txtJndi);
		
		// txtSchema
		txtSchema = new TextField();
		txtSchema.setStyleName("fieldRequired");
		txtSchema.setImmediate(false);
		txtSchema.setCaption("Esquema: *");
		txtSchema.setWidth("200px");
		txtSchema.setHeight("-1px");
		txtSchema.setInputPrompt("Schema");
		pnlFormulario.addComponent(txtSchema);
		
		// hlyBotonera
		hlyBotonera = buildHlyBotonera();
		pnlFormulario.addComponent(hlyBotonera);
		
		return pnlFormulario;
	}

	@AutoGenerated
	private HorizontalLayout buildHlyBotonera() {
		// common part: create layout
		hlyBotonera = new HorizontalLayout();
		hlyBotonera.setStyleName("buttoncust");
		hlyBotonera.setImmediate(false);
		hlyBotonera.setWidth("-1px");
		hlyBotonera.setHeight("-1px");
		hlyBotonera.setMargin(false);
		hlyBotonera.setSpacing(true);
		
		// btnGuardar
		btnGuardar = new Button();
		btnGuardar.setImmediate(true);
		btnGuardar.setWidth("-1px");
		btnGuardar.setHeight("-1px");
		btnGuardar.setCaption("");
		btnGuardar.setIcon(new ThemeResource("images/save_16_2.png"));
		btnGuardar.setDescription("Guardar");
		hlyBotonera.addComponent(btnGuardar);
		
		// btnEliminar
		btnEliminar = new Button();
		btnEliminar.setImmediate(true);
		btnEliminar.setCaption("");
		btnEliminar.setIcon(new ThemeResource("images/trash_16_2.png"));
		btnEliminar.setDescription("Eliminar");
		btnEliminar.setWidth("-1px");
		btnEliminar.setHeight("-1px");
		hlyBotonera.addComponent(btnEliminar);
		
		// btnTestConnection
		btnTestConnection = new Button();
		btnTestConnection.setImmediate(true);
		btnTestConnection.setCaption("");
		btnTestConnection.setIcon(new ThemeResource("images/connect_16_2.png"));
		btnTestConnection.setDescription("Test Connection");
		btnTestConnection.setWidth("-1px");
		btnTestConnection.setHeight("-1px");
		hlyBotonera.addComponent(btnTestConnection);
		
		return hlyBotonera;
	}

}
